package com.tianyi.controller.role;

import com.tianyi.controller.BaseController;
import com.tianyi.lib.model.WebResult;
import com.tianyi.lib.req.RoleReq;
import com.tianyi.model.SimpleLoginUser;
import com.tianyi.role.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * 角色controller
 *
 * @author cuixuhui
 * @date 2020/06/24
 */
@RestController
@RequestMapping("role")
public class RoleController extends BaseController {

    @Autowired
    private RoleService roleService;

    /**
     * 新增权限
     * @return
     */
    @RequestMapping(value = "addRole", method = RequestMethod.POST)
    public WebResult<?> addRole(@RequestBody RoleReq req){

        SimpleLoginUser loginUser = getUserFromRequest();
        if (Objects.isNull(loginUser)){
            return WebResult.error("用户未登陆");
        }
        //super.handLoginUser(req);
        return roleService.addRoleRecord(req);
    }

    /**
     * 删除权限
     * @param req
     * @return
     */
    @RequestMapping(value = "deleteRoleById", method = RequestMethod.GET)
    public WebResult<?> deleteRoleById(RoleReq req){

        SimpleLoginUser loginUser = getUserFromRequest();
        if (Objects.isNull(loginUser)){
            return WebResult.error("用户未登陆");
        }
        super.handLoginUser(req);
        return roleService.deleteRoleById(req);
    }

    /**
     * 修改权限
     * @return
     */
    @RequestMapping(value = "updateRole", method = RequestMethod.POST)
    public WebResult<?> updateRole(@RequestBody RoleReq req){

        SimpleLoginUser loginUser = getUserFromRequest();
        if (Objects.isNull(loginUser)){
            return WebResult.error("用户未登陆");
        }
        //super.handLoginUser(req);
        return roleService.updateRole(req);
    }

    /**
     * 修改权限
     * @return
     */
    @RequestMapping(value = "updateRoleNew", method = RequestMethod.POST)
    public WebResult<?> updateRoleMenuIds(@RequestBody RoleReq req){

        SimpleLoginUser loginUser = getUserFromRequest();
        if (Objects.isNull(loginUser)){
            return WebResult.error("用户未登陆");
        }

        return roleService.updateRoleNew(req);
    }

    /**
     * 查询权限下拉
     * @return
     */
    @RequestMapping(value = "getRolePullDown", method = RequestMethod.GET)
    public WebResult<?> getRolePullDown(){

        SimpleLoginUser loginUser = getUserFromRequest();
        if (Objects.isNull(loginUser)){
            return WebResult.error("用户未登陆");
        }
        return roleService.getRolePullDown();
    }

    /**
     * 查询权限分页
     * @return
     */
    @RequestMapping(value = "queryRolePage", method = RequestMethod.GET)
    public WebResult<?> queryRolePage(RoleReq req){

        SimpleLoginUser loginUser = getUserFromRequest();
        if (Objects.isNull(loginUser)){
            return WebResult.error("用户未登陆");
        }
        super.handLoginUser(req);
        return roleService.queryRolePage(req);
    }

    /**
     * 查询权限分页
     * @return
     */
    @RequestMapping(value = "getRoleDetail", method = RequestMethod.GET)
    public WebResult<?> getRoleDetail(RoleReq req){

        SimpleLoginUser loginUser = getUserFromRequest();
        if (Objects.isNull(loginUser)){
            return WebResult.error("用户未登陆");
        }
        super.handLoginUser(req);
        return roleService.getRoleDetail(req);
    }



}
